package com.randi.generator.util;

import com.randi.generator.entity.GenTable;
import com.randi.generator.entity.GenTableColumn;
import org.apache.velocity.VelocityContext;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class VelocityUtils {

    /**
     * 项目空间路径
     */
    private static final String PROJECT_PATH = "main/java";

    /**
     * mybatis空间路径
     */
    private static final String MYBATIS_PATH = "main/resources/mapper";

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext(GenTable genTable) {
        VelocityContext velocityContext = new VelocityContext();

        GenTableColumn column = genTable.getColumns().stream().filter(item -> item.getIsPrimaryKey() == 1).findFirst().get();

        velocityContext.put("tableName", genTable.getTableName());
        velocityContext.put("functionName", genTable.getFunctionName());
        velocityContext.put("ClassName", genTable.getClassName());
        velocityContext.put("className", StringUtils.uncapitalize(genTable.getClassName()));
        velocityContext.put("moduleName", genTable.getModuleName());
        velocityContext.put("BusinessName", StringUtils.capitalize(genTable.getBusinessName()));
        velocityContext.put("businessName", genTable.getBusinessName());
        velocityContext.put("packageName", genTable.getPackageName());
        velocityContext.put("incrementType", genTable.getIncrementType());
        velocityContext.put("primaryColumn", column);
        velocityContext.put("columns", genTable.getColumns());
        velocityContext.put("table", genTable);

        return velocityContext;
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList() {
        List<String> templates = new ArrayList<>();
        templates.add("vm/java/entity.java.vm");
        templates.add("vm/java/controller.java.vm");
        templates.add("vm/java/service.java.vm");
        templates.add("vm/java/serviceImpl.java.vm");
        templates.add("vm/java/mapper.java.vm");
        templates.add("vm/xml/mapper.xml.vm");
        templates.add("vm/js/api.js.vm");
        templates.add("vm/js/react.js.vm");
//        templates.add("vm/sql/sql.vm");
        return templates;
    }


    /**
     * 获取文件名
     */
    public static String getFileName(String template, GenTable genTable) {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genTable.getPackageName();
        // 模块名
        String moduleName = genTable.getModuleName();
        // 大写类名
        String className = genTable.getClassName();
        // 业务名称
        String businessName = genTable.getBusinessName();

        String javaPath = StringUtils.replace(packageName, ".", "/");

        if (template.contains("entity.java.vm")) {
            fileName = javaPath + "/entity/" + className + ".java";
        } else if (template.contains("controller.java.vm")) {
            fileName = javaPath + "/controller/" + className + "Controller.java";
        } else if (template.contains("service.java.vm")) {
            fileName = javaPath + "/service/I" + className + "Service.java";
        } else if (template.contains("serviceImpl.java.vm")) {
            fileName = javaPath + "/service/impl/" + className + "Service.java";
        } else if (template.contains("mapper.java.vm")) {
            fileName = javaPath + "/mapper/" + className + "Mapper.java";
        } else if (template.contains("mapper.xml.vm")) {
            fileName = javaPath + "/xml/mapper/" + moduleName+ "/" + className + "Mapper.xml";
        } else if (template.contains("api.js.vm")) {
            fileName = "/js/api/" + moduleName + "/" + businessName + ".js";
        } else if (template.contains("react.js.vm")) {
            fileName = "/js/" + moduleName + "/" + businessName + ".js";
        }

        return fileName;
    }


}
